//*************************************************
// Filename: verify_password.js
// 
// This JavaScript file contains a function for
// checking and validating the fields contained
// in the retrieve_password.php file.
//
//
// Author: Caleb Goff
// Date Created: 10/10/2009
//*************************************************

/**
 * @Desc Function called on a POST.
 *
 */
function postLoad(){
	resetElems();
}

/**
 * @Desc Resets all of the elements with the class name 'resets'.
 */
function resetElems(){

	document.getElementById("league_id").value = "NULL";
	var elems = document.getElementsByClassName("resets");

	for (var i = 0; i < elems.length; i++)
		elems[i].value = "";

}

/**
* @Desc Checks and validates email inputs
*
**/
function verifyEmail() {

	var league_id = document.getElementById("league_id").value;
	var email = document.getElementById("email_add").value;
	document.getElementById("team_verify").value = league_id + email;
	var verify = getHash(document.getElementById("team_verify"));

	if (echeck(email) == false) {
		Popup.showModal('popupBadEmailTeam',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	}
	// End input checks

	//Send these parameters to the PHP function on the server using
	//an HTTP post request
	var paramNames = "leagueId,teamEmail,verify";
	var paramVals  = league_id + "," + email + "," + verify;
	var url        = "get_team.php";
	
	//Wait for a response
	var response = postRequest(paramNames, paramVals, url);

	// Check if successful
	switch ((response-0)) {
		case 1:
		{
            // No team was found in the given league with provided email
			Popup.showModal('popupBadEmailTeam',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
			break;
		}
		default:
		{
            // Success
            Popup.showModal('popupEmailSuccess',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
			break;
		}
	}
}

/**
* @Desc Checks and validates security question inputs
*
**/
function verifyQuestion(team_id, league_id) {

	var answer = document.getElementById("q_answer").value;
    var key    = document.getElementById("key_verify").value;
    var hash   = getHash(document.getElementById("q_answer"));

	//Send these paramaters to the PHP function on the server using
	//an HTTP post request
	var paramNames = "leagueId,teamId,answer,key";
	var paramVals  = league_id + "," + team_id + "," + answer + "," + key;
	var url        = "check_answer.php";
	
	//Wait for a response
	var response = postRequest(paramNames, paramVals, url);

	// Check if login was successful
	switch ((response-0)) {
		case 1:
		{
            // Invalid team
			Popup.showModal('popupBadTeam',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
			break;
		}
        case 2:
        {
            // Incorrect answer
            Popup.showModal('popupBadAnswer',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
            break;
        }
		default:
		{
            // Success
            Popup.showModal('popupResetSuccess',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
			window.location = 'retrieve_password.php?mode=reset&key='+key+'&answer='+hash;
			break;
		}
	}
}

/**
* @Desc Checks and validates the resetting inputs
*
**/
function verifyReset(team_id, league_id) {

	var pwd = document.getElementById("password").value;
	var pwd_confirm = document.getElementById("password_confirm").value;

    // If confirmation and original password do not match
    if (!(pwd_confirm == pwd)) {
		return;
	} 
	else {
		pwd = getHash(document.getElementById("password"));
	}

	//Send these login paramaters to the PHP function on the server using
	//an HTTP post request
	var paramNames = "leagueId,teamId,teamPwd";
	var paramVals  = league_id + "," + team_id + "," + pwd;
	var url        = "reset_password.php";
	
	//Wait for a response
	var response = postRequest(paramNames, paramVals, url);

	// Check if login was successful
	switch ((response-0)) {
		default:
		{
            // Success
			window.location = 'index.php';
			break;
		}
	}
}
